﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZGEDrySaltery.Code;
using ZGEDrySaltery.DTO;
using ZGEDrySaltery.Model;

namespace ZGEDrySaltery.BLL
{
    public class OfferBLL
    {
        /// <summary>
        /// 写日志
        /// </summary>
        private Log log;

        #region 单例模式
        private static OfferBLL instance;

        public static OfferBLL GetInstance()
        {
            if (instance == null)
            {
                lock (typeof(OfferBLL))
                {
                    if (instance == null)
                    {
                        instance = new OfferBLL();
                    }
                }
            }
            return instance;
        }
        private OfferBLL()
        {
            log = LogFactory.GetLogger(this.GetType().ToString());
        }

        #endregion

        #region 获取分页商品列表
        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <param name="pagination">分页对象</param>
        /// <param name="offerpara">参数</param>
        /// <returns></returns>
        public List<OfferModel> GetOfferList(Pagination pagination, OfferPara offerpara)
        {
            int starindex = (pagination.page - 1) * pagination.rows;//起始页
            int pagesize = pagination.rows;//分页大小
            try
            {
                List<OfferModel> offList = DAL.OfferDAL.GetInstance().GetOfferList(starindex, pagesize, offerpara.pcid, offerpara.state, offerpara.title);
                pagination.records = DAL.OfferDAL.GetInstance().GetOfferCount(offerpara.pcid, offerpara.state, offerpara.title);
                return offList;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
            return null;
        }
        #endregion

        #region 获取商品详情
        /// <summary>
        /// 获取商品详情
        /// </summary>
        /// <param name="offerid">商品ID</param>
        /// <returns></returns>
        public OfferModel GetFormOffer(int offerid)
        {
            try
            {
                OfferModel offer = DAL.OfferDAL.GetInstance().GetFormOffer(offerid);
                return offer;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
            return null;
        }
        #endregion

        #region 提交
        public bool SubmitForm(OfferModel model, string keyValue)
        {
            bool flag = false;
            try
            {
                if (string.IsNullOrEmpty(keyValue))
                {
                    model.INSERTTIME = DateTime.Now;
                    model.MODIFYTIME = DateTime.Now;
                    flag = DAL.OfferDAL.GetInstance().Add(model);
                }
                else
                {
                    model.OFFERID = Convert.ToInt32(keyValue);
                    model.MODIFYTIME = DateTime.Now;
                    flag = DAL.OfferDAL.GetInstance().Update(model);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
            return flag;
        }
        #endregion
    }
}
